package com.example.extension.framework.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.extension.framework.ScenarioCapable;
import com.example.extension.framework.config.ExtensionConfigBean;
import com.example.extension.framework.config.TenantMirrorConfigBean;

/**
 * 
 * @author jie.deng
 */
@Component
public class MirrorScenarioCapableImpl implements ScenarioCapable {

    private static final Logger LOGGER = LoggerFactory.getLogger(MirrorScenarioCapableImpl.class);

    @Autowired
    private ExtensionConfigBean extensionConfig;
    @Autowired
    private TenantMirrorConfigBean mirrorConfig;

    @Override
    public String getScenarioCode(String tenantCode, Class<?> superType) {
        String scenarioCode = extensionConfig.getScenarioCode(tenantCode, superType);
        if (StringUtils.isBlank(scenarioCode) || ScenarioCapable.SCENARIO_CODE_DEFAULT.equals(scenarioCode)) {
            String mirrorTenantCode = mirrorConfig.getMirror(tenantCode);
            if (StringUtils.isNotBlank(mirrorTenantCode)) {
                String mirrorScenarioCode = extensionConfig.getScenarioCode(mirrorTenantCode, superType);
                if (StringUtils.isNotBlank(mirrorScenarioCode)
                    && !ScenarioCapable.SCENARIO_CODE_DEFAULT.equals(mirrorScenarioCode)) {
                    LOGGER.warn("找不到租户=【{}】扩展点=【{}】相关的配置，使用模拟租户【{}】", tenantCode, superType.getSimpleName(),
                        mirrorTenantCode);
                    scenarioCode = mirrorScenarioCode;
                }
            }
        }
        return scenarioCode;
    }

}
